using System;
using System.Reflection;

namespace Sedge.Core.Utils
{
	public static class TypeActivator
	{
		public static T Activate<T>(string assemblyFile, string name) where T : class 
		{
			T result = null;
			try
			{
				Assembly assembly = String.IsNullOrEmpty(assemblyFile) ? Assembly.GetExecutingAssembly() : Assembly.LoadFile(assemblyFile);
				Type type = null;
				Type[] types = assembly.GetTypes();
				foreach (Type testType in types)
				{
					if (testType.Name.Contains(name))
					{
						type = testType;
						break;
					}
				}

				if (type != null && typeof(T).IsAssignableFrom(type))
				{
					result = Activator.CreateInstance(type) as T;
				}
			}
			catch (Exception e)
			{
				ErrorHandler.Warning(e.Message);
			}

			return result;
		}
	}
}